home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_dos.lha / dos / sphsrc.v08 / MAT3mat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-26  |  2.8 KB  |  139 lines

  1. #include "HEADERS.h"
  2. /* Copyright 1988, Brown Computer Graphics Group.  All Rights Reserved. */
  3.  
  4. /* --------------------------------------------------------------------------
  5.  * This file contains routines that operate solely on matrices.
  6.  * -------------------------------------------------------------------------*/
  7.  
  8. #include "mat3defs.h"
  9.  
  10. #include "macros.h"
  11.  
  12. /* --------------------------  Static Routines    ---------------------------- */
  13.  
  14. /* -------------------------  Internal Routines  --------------------------- */
  15.  
  16. /* --------------------------  Public Routines    ---------------------------- */
  17.  
  18.  
  19. /*
  20.  * Sets a matrix to identity.
  21.  */
  22.  
  23. void
  24. MAT3identity (register MAT3mat mat)
  25. {
  26.    register int i;
  27.  
  28.    bzero (mat, sizeof(MAT3mat));
  29.    for (i = 0; i < 4; i++)
  30.       mat[i][i] = 1.0;
  31. }
  32.  
  33. /*
  34.  * Sets a matrix to zero.
  35.  */
  36.  
  37. void
  38. MAT3zero (MAT3mat mat)
  39. {
  40.    bzero (mat, sizeof(MAT3mat));
  41. }
  42.  
  43.  
  44. /*
  45.  * Copies one matrix to another.
  46.  */
  47.  
  48. void
  49. MAT3copy(MAT3mat to, MAT3mat from)
  50. {
  51.    bcopy (from, to, sizeof(MAT3mat));
  52. }
  53.  
  54. /*
  55.  * This multiplies two matrices, producing a third, which may the same as
  56.  * either of the first two.
  57.  */
  58.  
  59. void
  60. MAT3mult (result_mat, mat1, mat2)
  61. MAT3mat result_mat;
  62. register MAT3mat mat1, mat2;
  63. {
  64.    register int i, j;
  65.    MAT3mat    tmp_mat;
  66.  
  67.    for (i = 0; i < 4; i++)
  68.       for (j = 0; j < 4; j++)
  69.          tmp_mat[i][j] = (mat1[i][0] * mat2[0][j] +
  70.                mat1[i][1] * mat2[1][j] +
  71.                mat1[i][2] * mat2[2][j] +
  72.                mat1[i][3] * mat2[3][j]);
  73.    MAT3copy (result_mat, tmp_mat);
  74. }
  75.  
  76. /*
  77.  * This returns the transpose of a matrix.  The result matrix may be
  78.  * the same as the one to transpose.
  79.  */
  80.  
  81. void
  82. MAT3transpose (result_mat, mat)
  83. MAT3mat result_mat;
  84. register MAT3mat mat;
  85. {
  86.    register int i, j;
  87.    MAT3mat    tmp_mat;
  88.  
  89.    for (i = 0; i < 4; i++) 
  90.       for (j = 0; j < 4; j++) 
  91.          tmp_mat[i][j] = mat[j][i];
  92.  
  93.    MAT3copy (result_mat, tmp_mat);
  94. }
  95.  
  96.  
  97. /*
  98.  * This prints the given matrix to the given file pointer.
  99.  */
  100.  
  101. void
  102. MAT3print(mat, fp)
  103. MAT3mat mat;
  104. FILE    *fp;
  105. {
  106.    MAT3print_formatted(mat, fp, CNULL, CNULL, CNULL, CNULL);
  107. }
  108.  
  109. /*
  110.  * This prints the given matrix to the given file pointer.
  111.  * use the format string to pass to fprintf.  head and tail
  112.  * are printed at the beginning and end of each line.
  113.  */
  114.  
  115. void
  116. MAT3print_formatted(mat, fp, title, head, format, tail)
  117. MAT3mat mat;
  118. FILE    *fp;
  119. char    *title, *head, *format, *tail;
  120. {
  121.    register int i, j;
  122.  
  123.    /* This is to allow this to be called easily from a debugger */
  124.    if (fp == NULL) fp = stderr;
  125.  
  126.    if (title  == NULL)    title  = "MAT3 matrix:\n";
  127.    if (head   == NULL)    head   = "  ";
  128.    if (format == NULL)    format = "%#8.4lf  ";
  129.    if (tail   == NULL)    tail   = "\n";
  130.  
  131.    (void) fprintf(fp, title);
  132.  
  133.    for (i = 0; i < 4; i++) {
  134.       (void) fprintf(fp, head);
  135.       for (j = 0; j < 4; j++) (void) fprintf(fp, format, mat[i][j]);
  136.       (void) fprintf(fp, tail);
  137.    }
  138. }
  139.